class SendData{
    /*
        发送数据
    */

    constructor(){

        // 导航栏
        this.id_a_send_data = "#a_send_data";

        this.id_container_send_data = "#container_send_data";
        this.id_btn_send_data = "#btn_send_data";

        // 导出数据
        this.btn_exprot_to_excel = $("#btn_export_to_excel");

        // 数据表格
        this.data_table = $("#data_table");
        this.table_title_bar = $("#table_head");
        this.table_body_bar = $("#table_body");


        // 设置
        this.txt_turn_page_rate = $("#txtTurnPageRate");

        // 进度
        this.process_bar = $("#container_export_process");
        this.process_total = $("#span_all_turn_page_time");
        this.process_current = $("#span_current_turn_page_time");
        this.process_btn_stop = $("#btn_process_stop");
        this.process_btn_done = $("#btn_process_done");


        this.warning_confirm = new WarningConfirmBar();
        
        this.options = {
            'current_id': 0,
            'data_refresh_type': null,
            'all_turn_page_times': null,
            'current_turn_page_times': null,
            'turn_page_rate': 3,
            'is_process_done': true,
        }

        
    }

    reset_options(){
        // this.options = {
        //     'current_id': 0,
        //     'data_refresh_type': null,
        //     'all_turn_page_times': null,
        //     'current_turn_page_times': null,
        //     // 'turn_page_rate': 3,
        //     'is_process_done': true,
        // }

        this.options.current_id = 0;
        this.options.data_refresh_type = null;
        this.options.all_turn_page_times = null;
        this.options.current_turn_page_times = null;
        this.options.is_process_done = true;
    }

    hide_send_data(){
        $(this.id_container_send_data).addClass("d-none");
    }

    display_send_data(){
        $(this.id_container_send_data).removeClass("d-none");
    }

    register_SendData(){
        let mc_this = this;
        this.warning_confirm.register_warning();

        $(this.id_a_send_data).click(function(){
            mc_Navigation.reset_nav();
            mc_Navigation.display_send_data("send_data");
        });

        $(this.id_btn_send_data).click(function(){
            let return_obj = null;
            return_obj = web_info_object.return_submit_obj();
            if (return_obj === false){
                mc_this.warning_confirm.display_warning("数据未完成");
                return false;
            }else{
                $.get("http://127.0.0.1:8000/ProjectManagement/api/sendBasicInfo/", return_obj, function(data, status){
                        console.log('status:' + status);
                        console.log('data_status:' + data['status']);
                        console.log('task_id:' + data['task_id']);
                });
            }
        });
        
        this.btn_exprot_to_excel.click(function(){
            mc_this.reset_table();
            mc_this.show_process();
            mc_this.process_start();
            mc_this.create_table();
        });

        this.txt_turn_page_rate.change(function(){
            // 检查输入数据的合法性
            let input_value = $(this).val();
            if (isNaN(input_value) || input_value > 10 || input_value < 0){
                // 输入非法
                mc_this.warning_confirm.display_warning("非法输入");
                $(this).val(3);
                return;
            }else if(input_value === ""){
                mc_this.warning_confirm.display_warning("输入非空");
                $(this).val(3);
                return;
            }

            // mc_this.txt_turn_page_rate = input_value;
            mc_this.options.turn_page_rate = input_value;
        });

        // 任务进度
        this.process_btn_stop.click(function(){
            mc_this.process_done();
            
        });

        this.process_btn_done.click(function(){
            mc_this.hide_process();
            mc_this.reset_process();
        });
    }


    create_table(){
        /*
            1. 找到当前的数据方法
            2. 填充内容
        */

        let method = web_info_object.basic_info.data_acquisition_method;

        if (method === GlobalValue.DATA_METHOD_SIMPLE_LIST){

            for (let i = 0; i < mc_GetData.simple_list.data_set.data_set_list.list_length; i++) {
                const element = mc_GetData.simple_list.data_set.data_set_list.list_space[i];
                let title = $("<th scope='col'>"+ element.name +"</th>");
                this.table_title_bar.append(title);
            }


            let msg = Message.get_normal_msg();
            msg.todo = "get_table_data";
            msg.data_binding_xpath = mc_GetData.simple_list.data_binding_set.xpath_binding;
            msg.data_binding_sub_list = mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list;
            msg.data_set_list = mc_GetData.simple_list.data_set.data_set_list.list_space;
            Message.msg_send_to_background(msg);

        }else if(method === GlobalValue.DATA_METHOD_STRUCTURE){
            let msg = Message.get_normal_msg();
            msg.todo = "get_table_data_structure";
            msg.data_binding_xpath = mc_GetData.structure.data_binding_set.xpath_binding;
            msg.data_binding_sub_list = mc_GetData.structure.data_binding_set.sub_xpath_binding_list;

            msg.structure = mc_GetData.structure.html_analyzer.element_list.get_selected_node_list_in_xpath();


            // 构造表头
            for (let i = 0; i < msg.structure.length; i++) {
                const element = msg.structure[i];
                let base_name = element.xpath.split("/").pop();
                for (let j = 0; j < element.selected_attr_list.length; j++) {
                    const attr = element.selected_attr_list[j];
                    let name = base_name + "." + attr.name;
                    let title = $("<th scope='col'>"+ name +"</th>");
                    this.table_title_bar.append(title);
                }
            }

            Message.msg_send_to_background(msg);
        }
    }

    reset_table(){
        this.table_title_bar.empty();
        let title_num = $("<th scope='col'>#</th>");
        this.table_title_bar.append(title_num);

        this.table_body_bar.empty();
        this.reset_options();

        // 设置数据刷新选项
        this.options.data_refresh_type = mc_GetData.simple_list.data_binding_set.data_refresh_type;
        if (this.options.data_refresh_type === GlobalValue.DATA_REFRESH_TURN_PAGE){
            this.options.all_turn_page_times = mc_GetData.simple_list.data_binding_set.turn_page_times;
        }else if (this.options.data_refresh_type === GlobalValue.DATA_REFRESH_WATERFALL_BTN){
            this.options.all_turn_page_times = mc_GetData.simple_list.data_binding_set.waterfall_btn_times;
        }else if (this.options.data_refresh_type === GlobalValue.DATA_REFRESH_WATERFALL){
            this.options.all_turn_page_times = mc_GetData.simple_list.data_binding_set.waterfall_times;
        }
        this.options.current_turn_page_times = 0;
    }

    set_data_list(data_list){
        for (let i = 0; i < data_list.length; i++) {
            /*
                <tr>
                    <th scope="row">1</th>
                    <td>content</td>
                </tr>
            */
            const element = data_list[i];
            let tr = $("<tr></tr>");
            // let index = i+1;
            let th = $("<th scope='row'>"+ (this.options.current_id+1) +"</th>");
            this.options.current_id++;
            
            tr.append(th);
            let regStr = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;

            for (let j = 0; j < element.length; j++) {
                let box_content = element[j].replace(regStr, '[表情]');
                let td = $("<td>"+ box_content +"</td>");
                tr.append(td);
            }
            this.table_body_bar.append(tr);
        }
    }

    fill_table_content(data_list, sub_xpath_list){
        for (let i = 0; i < data_list.length; i++) {
            /*
                <tr>
                    <th scope="row">1</th>
                    <td>content</td>
                </tr>
            */
            const element = data_list[i];
            let tr = $("<tr></tr>");
            // let index = i+1;
            let th = $("<th scope='row'>"+ (this.options.current_id+1) +"</th>");
            this.options.current_id++;
            
            tr.append(th);
            let regStr = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;

            for (let j = 0; j < element.length; j++) {
                let box_content = element[j].replace(regStr, '[表情]');
                let td = $("<td>"+ box_content +"</td>");
                tr.append(td);
            }
            this.table_body_bar.append(tr);
        }

        // 检查任务是否已经停止
        if (this.options.is_process_done == true){
            this.update_process();
            this.stop_get_data();
            return;
        }

        // 检查数据刷新
        if (this.options.data_refresh_type === GlobalValue.DATA_REFRESH_TURN_PAGE){
            // 检查翻页次数
            if (this.options.current_turn_page_times !== parseInt(this.options.all_turn_page_times)){
                this.update_process();
                this.options.current_turn_page_times++;
                let msg = Message.get_normal_msg();
                msg.todo = "turn_page_and_get_data";
                msg.btn_xpath = mc_GetData.simple_list.data_binding_set.turn_page_xpath;
                msg.btn_scrollTop = mc_GetData.simple_list.data_binding_set.turn_page_scrolltop;
                msg.data_binding_xpath = mc_GetData.simple_list.data_binding_set.xpath_binding;
                msg.data_binding_sub_list = mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list;
                msg.data_binding_max = mc_GetData.simple_list.data_binding_set.max_xpath_binding;
                msg.data_set_list = mc_GetData.simple_list.data_set.data_set_list.list_space;
                msg.turn_page_rate = this.options.turn_page_rate;
                Message.msg_send_to_background(msg);
            }else{
                this.update_process();
                this.process_done();
                this.export_data_to_excel();
                this.reset_options();

                // 更新data binding信息
                mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list = sub_xpath_list;
                mc_GetData.simple_list.data_binding_set.update();
                
            }
        }else if(this.options.data_refresh_type === GlobalValue.DATA_REFRESH_SINGLE_PAGE){
            this.export_data_to_excel();
            this.reset_options();
            // 更新data binding信息
            mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list = sub_xpath_list;
            mc_GetData.simple_list.data_binding_set.update();
        }
    }

    fill_structure_table_content(data_list){
        for (let i = 0; i < data_list.length; i++) {
            const data_set = data_list[i];
            let tr = $("<tr></tr>");
            let index = i+1;
            let th = $("<th scope='row'>"+ index +"</th>");
            tr.append(th);

            for (let j = 0; j < data_set.length; j++) {
                const tag = data_set[j];
                for (let k = 0; k < tag.length; k++) {
                    let td = $("<td>"+ tag[k] +"</td>");
                    tr.append(td);    
                }
                
            }
            this.table_body_bar.append(tr);
        }
    }

    fill_waterfall_btn_content(data_list, sub_xpath_list){
        // 检查任务是否已经停止
        if (this.options.is_process_done == true){
            this.set_data_list(data_list);
            this.update_process();
            this.stop_get_data();
            return;
        }

        if(this.options.data_refresh_type === GlobalValue.DATA_REFRESH_WATERFALL_BTN){
            if (this.options.current_turn_page_times !== parseInt(this.options.all_turn_page_times)){
                this.update_process();
                this.options.current_turn_page_times++;
                let msg = Message.get_normal_msg();
                msg.todo = "get_waterfall_btn_data";
                msg.btn_xpath = mc_GetData.simple_list.data_binding_set.waterfall_btn_xpath;
                msg.btn_scrollTop = mc_GetData.simple_list.data_binding_set.waterfall_btn_scrolltop;
                msg.data_binding_xpath = mc_GetData.simple_list.data_binding_set.xpath_binding;
                msg.data_binding_max = mc_GetData.simple_list.data_binding_set.max_xpath_binding;
                msg.data_set_list = mc_GetData.simple_list.data_set.data_set_list.list_space;
                msg.turn_page_rate = this.options.turn_page_rate;
                Message.msg_send_to_background(msg);
            }else{
                this.update_process();
                this.process_done();
                this.set_data_list(data_list);
                this.export_data_to_excel();
                this.reset_options();

                mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list = sub_xpath_list;
                mc_GetData.simple_list.data_binding_set.update();
            }
        }
    }

    fill_waterfall_content(data_list, sub_xpath_list){

        // 检查任务是否已经停止
        if (this.options.is_process_done == true){
            this.set_data_list(data_list);
            this.update_process();
            this.stop_get_data();
            return;
        }
        
        if (this.options.current_turn_page_times !== parseInt(this.options.all_turn_page_times)){
            this.options.current_turn_page_times++;
            let msg = Message.get_normal_msg();
            msg.todo = "get_waterfall_data";
            msg.btn_xpath = mc_GetData.simple_list.data_binding_set.waterfall_btn_xpath;
            msg.btn_scrollTop = mc_GetData.simple_list.data_binding_set.waterfall_btn_scrolltop;
            msg.data_binding_xpath = mc_GetData.simple_list.data_binding_set.xpath_binding;
            msg.data_binding_max = mc_GetData.simple_list.data_binding_set.max_xpath_binding;
            msg.data_set_list = mc_GetData.simple_list.data_set.data_set_list.list_space;
            msg.turn_page_rate = this.options.turn_page_rate;
            Message.msg_send_to_background(msg);
        }else{
            this.update_process();
            this.process_done();
            this.set_data_list(data_list);
            this.export_data_to_excel();
            this.reset_options();

            mc_GetData.simple_list.data_binding_set.sub_xpath_binding_list = sub_xpath_list;
            mc_GetData.simple_list.data_binding_set.update();
        }
    }

    export_data_to_excel(){
        var elt = document.getElementById('data_table');
        let wb = XLSX.utils.table_to_book(elt, {sheet:"Data", raw:true});
        return XLSX.writeFile(wb, "data.xlsx");
    }

    // 任务进度
    show_process(){
        this.process_bar.removeClass("d-none");
    }

    hide_process(){
        this.process_bar.addClass("d-none");
    }

    reset_process(){
        /*
            重置任务进度
        */
        this.process_total.text(0);
        this.process_current.text(0);
        this.process_btn_done.addClass("d-none");
        this.process_btn_stop.addClass("d-none");
    }

    process_start(){
        this.options.is_process_done = false;
        this.process_total.text(this.options.all_turn_page_times);
        this.process_btn_stop.removeClass("d-none");
    }

    process_done(){
        this.options.is_process_done = true;
        this.process_btn_stop.addClass("d-none");
        this.process_btn_done.removeClass("d-none");
    }

    update_process(){
        this.process_current.text(this.options.current_turn_page_times);
    }

    stop_get_data(){
        /*
            停止任务
        */
        this.process_done();
        this.export_data_to_excel();
        this.reset_options();
        mc_GetData.simple_list.data_binding_set.update();
    }


}